package com.sgb.kjwl.view.ui.fragment.manage

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppLazyFragment
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.FragmentLogisticsOrderBinding
import com.sgb.link.entity.PickFilterEntity
import com.swgk.core.event.EventConstant
import com.sgb.kjwl.view.event.LogisticsEvent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class LogisticsTransportOrderFragment : AppLazyFragment() {

    private lateinit var mBinding : FragmentLogisticsOrderBinding
    private var mTitles : MutableList<PickFilterEntity<Any>> ?= null
    private lateinit var mAdapter : LogisticsPageAdapter

    companion object{
        const val TAG = "agentLeaseOrderFragment"
        const val KEY_DATAS = "key_datas"
        /**
         * 二级标题
         */
        fun newInstance()  : LogisticsTransportOrderFragment{
            val fragment = LogisticsTransportOrderFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (mTitles == null){
            mTitles = ArrayList()
        }
        val list = mutableListOf<PickFilterEntity<Any>>()
        val shippingOrder = PickFilterEntity<Any>()
        shippingOrder.no = "9"
        shippingOrder.name = "承运订单(0)"
        shippingOrder.num = 0
        val transportationOrder  = PickFilterEntity<Any>()
        transportationOrder.no = "10"
        transportationOrder.name = "货运订单(0)"
        transportationOrder.num = 0
        list.add(shippingOrder)
        list.add(transportationOrder)
        mTitles = list
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?,
    ): View {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_logistics_order, container, false)
        mAdapter = LogisticsPageAdapter(childFragmentManager, mTitles!!)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = 1
        mBinding.viewPage.currentItem = 0
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        return mBinding.root
    }

    override fun onLazyLoad() {
        //刷新第二级的第一个tab
        val fragment = childFragmentManager.findFragmentByTag(
                "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 0)
        if (fragment != null && fragment is AgentLeaseOrderStateFragment){
            fragment.initData()
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun refresh(event: LogisticsEvent) {
        if (event.messageTag == EventConstant.EVENT_ORDER_DRIVER_TOTAL_LIST) {
            var number :Int = event.messageData as Int
            var tabe1 :String =mBinding.tablayout.getTabAt(0)?.text.toString()
            var bracket: String = tabe1!!.substring(tabe1!!.indexOf("("), tabe1.indexOf(")") + 1)
            if (number>99){
                tabe1 = tabe1?.replace(bracket, "("+"99+"+")")
            }else{
                tabe1 = tabe1?.replace(bracket, "($number)")
            }
            mBinding.tablayout.getTabAt(0)?.text=tabe1
        }else if (event.messageTag == EventConstant.EVENT_ORDER_OWNER_TOTAL_LIST){
            var number :Int = event.messageData as Int
            var tabe2 :String =mBinding.tablayout.getTabAt(1)?.text.toString()
            var bracket: String = tabe2!!.substring(tabe2!!.indexOf("("), tabe2.indexOf(")") + 1)
            if (number>99){
                tabe2 = tabe2?.replace(bracket, "("+"99+"+")")
            }else{
                tabe2 = tabe2?.replace(bracket, "($number)")
            }
            mBinding.tablayout.getTabAt(1)?.text=tabe2
        }
    }
    class LogisticsPageAdapter(fm : FragmentManager, titles : MutableList<PickFilterEntity<Any>>) : FragmentPagerAdapter(fm) {
        private var mTitles : MutableList<PickFilterEntity<Any>> ?= null
        init {
            this.mTitles = titles
        }
        override fun getCount(): Int {
            if (mTitles != null){
                return mTitles!!.size
            }
            return 0
        }

        override fun getItem(position: Int): Fragment {
            var no = mTitles!![position].no
            when(no) {
                "9" -> { //出租订单--待接单、待运输
                    return AgentTransportOrderDriverFragment.newInstance()
                }
                "10" -> { //租入订单--待确认、待付款、待验收
                    return AgentTransportOrderOwnerFragment.newInstance()

                }
            }
            return OtherInquiryFragment.newInstance()
        }

        override fun getPageTitle(position: Int): CharSequence {
            if (mTitles != null){
                return mTitles!![position].name
            }
            return ""
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }
}